package com.leo.script;

import lombok.SneakyThrows;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.SimpleBindings;
import java.math.BigDecimal;
import java.util.List;

public class GroovyUtils {

    @SneakyThrows
    public static void main(String[] args) {
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("groovy");
        SimpleBindings bindings = new SimpleBindings();
        bindings.put("a", new BigDecimal("0.1"));
        bindings.put("b", new BigDecimal("0.2"));
        Object result = engine.eval("a + b", bindings);
        System.out.println(result);

        bindings = new SimpleBindings();
        bindings.put("list", List.of(new BigDecimal("0.1"), new BigDecimal("0.2")));
        result = engine.eval("list.sum()", bindings);
        System.out.println(result);
    }
}
